﻿#include <stdio.h>
#include <stdlib.h>

#include <iostream>

#include "facedetect.h"
// #include "memory.h"

long ReadFile(const char* pFileName, char* pData, long MaxSize) {
    FILE* fpFile;
    long lSize, lReadCount;
    fpFile = fopen(pFileName, "rb");
    fseek(fpFile, 0l, SEEK_END);
    lSize = ftell(fpFile);
    rewind(fpFile);

    if (lSize > MaxSize) {
        lSize = MaxSize;
    }
    lReadCount = fread(pData, 1, lSize, fpFile);
    fclose(fpFile);
    return lReadCount;
}
int WriteFile(const char* pFileName, char* pData, long Size) {
    FILE* fp;
    fp = fopen(pFileName, "wb");
    fwrite(pData, Size, 1, fp);
    fclose(fp);
    return (0);
}

int main(int argc, char** argv) {
    if (argc != 2) {
        printf("Usage: %s <image_file_name>\n", argv[0]);
        return -1;
    }
    long MaxJpgLen = 2 * 1024 * 1024;
    char* pJpgData = (char*)malloc(MaxJpgLen);
    long JpgLen = ReadFile(argv[1], pJpgData, MaxJpgLen);
    int FaceCount = FaceDetect((unsigned char*)pJpgData, JpgLen);
    std::cout << "FaceDetect count:" << FaceCount << std::endl;
    if (FaceCount == 1) {
        WriteFile("result_face.jpg", pJpgData, JpgLen);
    }
    free(pJpgData);
    return 0;
}